package com.xiaoke.api.controller;

import com.xiaoke.api.service.*;
import com.xiaoke.entity.*;
import com.xiaoke.shiro.SecurityService;
import com.xiaoke.util.common.AjaxMsg;
import com.xiaoke.util.common.ValidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsPriceService goodsPriceService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CouponService couponService;

    //@PostMapping("/add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMsg add(String goodsId, Short number, String goodsPriceId, HttpServletRequest request) {
        if (ValidUtil.isEmpty(goodsId, number, goodsPriceId))
            return new AjaxMsg(AjaxMsg.FAILURE, "参数不能为空！");

        User user = securityService.getCurrentUser();
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);
        Goods goods = goodsService.selectByPrimaryKey(goodsId);
        GoodsPrice goodsPrice = goodsPriceService.selectByPrimaryKey(goodsPriceId);


        Cart cart = new Cart();
        cart.setUserId(user.getId());
        cart.setGoodsId(goodsId);
        cart.setGoodsSn(goods.getGoodsSn());
        cart.setGoodsName(goods.getName());
        cart.setMarketPrice(goods.getMarketPrice());
        cart.setRetailPrice(goodsPrice.getPrice());
        cart.setNumber(number);
        cart.setGoodsPriceId(goodsPriceId);
        cart.setShopId(shop.getId());
        cartService.insert(cart);
        Integer count = cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId());

        return new AjaxMsg(count);
    }

    @GetMapping("/index")
    public AjaxMsg index(HttpServletRequest request) {
        User user = securityService.getCurrentUser();
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);

        List<Map<String, Object>> cartList = cartService.findByUserAndShop(user.getId(), shop.getId());
        Map res = new HashMap();
        res.put("cartList", cartList);
        Integer count = cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId());
        res.put("cartTotal", count);
        return new AjaxMsg(res);
    }

    @PostMapping("/update")
    public AjaxMsg update(HttpServletRequest request, String id, Short number) {
        User user = securityService.getCurrentUser();
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);

        Cart cart = cartService.selectByPrimaryKey(id);
        cart.setNumber(number);
        cartService.updateByPrimaryKeySelective(cart);

        List<Map<String, Object>> cartList = cartService.findByUserAndShop(user.getId(), shop.getId());
        Map res = new HashMap();
        res.put("cartList", cartList);
        Integer count = cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId());
        res.put("cartTotal", count);
        return new AjaxMsg(res);
    }

    @PostMapping("/delete")
    public AjaxMsg delete(String ids,HttpServletRequest request) {
        User user = securityService.getCurrentUser();
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);

        String[] id = ids.split(",");
        cartService.delete(id);
        List<Map<String, Object>> cartList = cartService.findByUserAndShop(user.getId(), shop.getId());
        Map res = new HashMap();
        res.put("cartList", cartList);
        Integer count = cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId());
        res.put("cartTotal", count);
        return new AjaxMsg(res);
    }

    @PostMapping("/checkout")
    public AjaxMsg checkout(HttpServletRequest request,String addressId,String couponId) {
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);
        User user = securityService.getCurrentUser();
       // Map<String,Object> address = addressService.getDefaultAddress(user.getId());
        Map<String,Object> address = addressService.findById(addressId);
        //Integer coupons = couponService.countByUserAndShop(user.getId(), shop.getId());
        List coupons=couponService.findByUserAndShop(user.getId(), shop.getId());

        Coupon checkedCoupon=couponService.selectByPrimaryKey(couponId);
        //商品信息
        List<Map<String, Object>> goods = cartService.findByChecked(user.getId(), shop.getId());
        Map<String, Object> prices = cartService.sumTotalPriceByChecked(user.getId(), shop.getId());

        //默認地址

        Map res = new HashMap();
        res.put("address", address);
        res.put("coupons", coupons);
        res.put("checkedCoupon", checkedCoupon);
        res.put("prices", prices);
        res.put("goods", goods);
        return new AjaxMsg(res);
    }

    @PostMapping("/checked")
    public AjaxMsg checked(String id, String isChecked, HttpServletRequest request) {
        if (ValidUtil.isEmpty(id))
            return new AjaxMsg(AjaxMsg.FAILURE, "参数不能为空！");
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);
        User user = securityService.getCurrentUser();

        Cart cart = cartService.selectByPrimaryKey(id);
        cart.setChecked(isChecked);
        cartService.updateByPrimaryKeySelective(cart);
        BigDecimal sum = cartService.sumByChecked(user.getId(), shop.getId());
        List<Map<String, Object>> cartList = cartService.findByUserAndShop(user.getId(), shop.getId());
        Map res = new HashMap();
        res.put("cartList", cartList);
        Integer count = cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId());
        res.put("cartTotal", count);
        return new AjaxMsg(res);
    }

    @RequestMapping("/goodsCount")
    public AjaxMsg goodsCount(HttpServletRequest request) {
        String appId = request.getHeader("appId");
        Shop shop = shopService.findByAppId(appId);
        User user = securityService.getCurrentUser();

        return new AjaxMsg(cartService.countByUserAndShopAndUnChecked(user.getId(), shop.getId()));
    }
}
